package com.web.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.mall.entity.TbItemCat;
import com.web.mall.req.TbItemCatReq;
import com.web.mall.service.ITbItemCatService;
import com.web.mall.utils.ResponseStatus;
import com.web.mall.utils.ResultBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author s1x
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/itemCat")
public class TbItemCatController {
    @Autowired
    ITbItemCatService tbItemCatService;

    // 分页条件查询
    @GetMapping("/getByCondition")
    public ResultBean getByCondition(TbItemCatReq tbItemCat) {
        System.out.println("\n分页条件查询tbBrand:" + tbItemCat);
        //1. 分页拦截，重构分页sql语句
        Page pageInfo = new Page(tbItemCat.getPage(), tbItemCat.getSize());
        LambdaQueryWrapper<TbItemCat> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(tbItemCat.getName()), TbItemCat::getName, tbItemCat.getName());
        wrapper.eq(!ObjectUtils.isEmpty(tbItemCat.getParentId()), TbItemCat::getParentId, tbItemCat.getParentId());
        tbItemCatService.page(pageInfo, wrapper);
        System.out.println("pageInfo:" + pageInfo);
        return ResultBean.ok(pageInfo);
    }

    // 查找不同级别的分类，有1级，2级，3级三种
    @GetMapping("/findChildrenByType/{type}")
    public ResultBean findChildrenByType(@PathVariable("type") Integer type) {
        LambdaQueryWrapper<TbItemCat> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(type) && type == 2) {
            // 查一级
            wrapper.eq(TbItemCat::getParentId, 0);
            return ResultBean.ok(tbItemCatService.list(wrapper));
        } else if (!ObjectUtils.isEmpty(type) && type == 3) {
            // 查二级
            wrapper.eq(TbItemCat::getParentId, 0);
            List<TbItemCat> list = tbItemCatService.list(wrapper);
            List<Long> ids = new ArrayList<>();
            for (TbItemCat c : list) {
                ids.add(c.getId());
            }
            LambdaQueryWrapper<TbItemCat> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.in(!ids.isEmpty(), TbItemCat::getParentId, ids);
            return ResultBean.ok(tbItemCatService.list(wrapper2));
        }
        return ResultBean.error();
    }

    // 根据id删除
    @GetMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Integer id) {
        LambdaQueryWrapper<TbItemCat> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(id), TbItemCat::getParentId, id);
        List<TbItemCat> list = tbItemCatService.list(wrapper);
        if (list != null && list.size() > 0) {
            return ResultBean.fail(ResponseStatus.DELETE_ERROR);
        }
        return ResultBean.ok(tbItemCatService.removeById(id));
    }


    // 新增或更新
    @PostMapping("/addOrUpdate")
    public ResultBean addOrUpdate(@RequestBody TbItemCat tbItemCat) {
        return ResultBean.ok(tbItemCatService.saveOrUpdate(tbItemCat));
    }

}